package com.yylending.las.base;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.tbruyelle.rxpermissions.RxPermissions;
import com.umeng.analytics.MobclickAgent;
import com.yyfax.common.toast.CompatToast;
import com.yyfax.infrastructure.mvp.ViewFragment;
import com.yylending.las.R;
import com.yylending.las.common.widgets.CommonDialog;
import com.yylending.las.common.widgets.LoadingProgressDialog;

/**
 * Created by HenryZhang on 2016/10/25.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public abstract class BaseFragment extends ViewFragment {
	/**
	 * The fragment argument representing the section number for this fragment.
	 */
	protected final static String ARG_SECTION_NUMBER = "section_number";

	private CommonDialog mDialog;
	protected RxPermissions mRxPermissions;

	public BaseFragment() {
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mRxPermissions = new RxPermissions(getActivity());
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(getClass().getSimpleName());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(getClass().getSimpleName());
	}

	@Override
	public void onDestroy() {
		dismissCommonDialog();
		mDialog = null;
		super.onDestroy();
	}

	protected void showToast(int id) {
		CompatToast.showShortToast(getActivity().getApplicationContext(), getString(id));
	}

	protected void showToast(String text) {
		CompatToast.showShortToast(getActivity().getApplicationContext(), text);
	}

	protected void showLongToast(String text) {
		CompatToast.showLongToast(getActivity().getApplicationContext(), text);
	}

	protected void showLongToast(int id) {
		CompatToast.showLongToast(getActivity().getApplicationContext(), getString(id));
	}

	/**
	 * 弹出两个按钮的提示框
	 *
	 * @param title         提示标题
	 * @param content       提示内容
	 * @param confirmString 确认按钮
	 * @param cancelString  取消按钮
	 * @param listener      按钮事件监听
	 * @param cancelAble
	 */
	public void showTwoBtnDialog(String title, String content, String confirmString, String cancelString,
                                 CommonDialog.ButtonClickListener listener, boolean cancelAble) {
		dismissCommonDialog();
		mDialog = new CommonDialog(getActivity(), title, content, confirmString, cancelString, listener, cancelAble);
		mDialog.show();
	}

	/**
	 * 弹出两个按钮的提示框
	 *
	 * @param title         提示标题
	 * @param content       提示内容
	 * @param confirmString 确认按钮
	 * @param cancelString  取消按钮
	 */
	public void showTwoBtnDialog(String title, String content, String confirmString, String cancelString) {
		showTwoBtnDialog(title, content, confirmString, cancelString, null, true);
	}

	/**
	 * 弹出两个按钮的提示框
	 *
	 * @param content       提示内容
	 * @param confirmString 确认按钮
	 * @param cancelString  取消按钮
	 */
	public void showTwoBtnDialog(String content, String confirmString, String cancelString) {
		showTwoBtnDialog(null, content, confirmString, cancelString, null, true);
	}

	/**
	 * 弹出两个按钮的提示框
	 *
	 * @param titleId    提示标题字符串id
	 * @param contentId  提示内容字符串id
	 * @param confirmId  确认按钮字符串id
	 * @param cancelId   取消按钮字符串id
	 * @param listener   按钮事件监听
	 * @param cancelAble
	 */
	public void showTwoBtnDialog(int titleId, int contentId, int confirmId, int cancelId,
			CommonDialog.ButtonClickListener listener, boolean cancelAble) {
		showTwoBtnDialog(getString(titleId), getString(contentId), getString(confirmId), getString(cancelId), listener, cancelAble);
	}

	/**
	 * 弹出两个按钮的提示框
	 *
	 * @param contentId  提示内容字符串id
	 * @param confirmId  确认按钮字符串id
	 * @param cancelId   取消按钮字符串id
	 * @param listener   按钮事件监听
	 * @param cancelAble
	 */
	public void showTwoBtnDialog(int contentId, int confirmId, int cancelId,
			CommonDialog.ButtonClickListener listener, boolean cancelAble) {
		showTwoBtnDialog(null, getString(contentId), getString(confirmId), getString(cancelId), listener, cancelAble);
	}

	/**
	 * 弹出两个按钮的提示框
	 *
	 * @param content 提示内容字符串
	 * @param confirm  确认按钮字符串
	 * @param cancel   取消按钮字符串
	 * @param listener   按钮事件监听
	 * @param cancelAble
	 */
	public void showTwoBtnDialog(String content, String confirm, String cancel,
			CommonDialog.ButtonClickListener listener, boolean cancelAble) {
		showTwoBtnDialog(null, content, confirm, cancel, listener, cancelAble);
	}

	/**
	 * 弹出两个按钮的提示框
	 *
	 * @param titleId   提示标题字符串id
	 * @param contentId 提示内容字符串id
	 * @param confirmId 确认按钮字符串id
	 * @param cancelId  取消按钮字符串id
	 */
	public void showTwoBtnDialog(int titleId, int contentId, int confirmId, int cancelId) {
		showTwoBtnDialog(getString(titleId), getString(contentId), getString(confirmId), getString(cancelId), null, true);
	}

	/**
	 * 弹出两个按钮的提示框
	 *
	 * @param contentId 提示内容字符串id
	 * @param confirmId 确认按钮字符串id
	 * @param cancelId  取消按钮字符串id
	 */
	public void showTwoBtnDialog(int contentId, int confirmId, int cancelId) {
		showTwoBtnDialog(null, getString(contentId), getString(confirmId), getString(cancelId), null, true);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param title         提示标题
	 * @param content       提示内容
	 * @param confirmString 确认按钮
	 * @param listener      按钮事件监听
	 * @param cancelAble
	 */
	public void showOneBtnDialog(String title, String content, String confirmString, CommonDialog.ButtonClickListener listener, boolean cancelAble) {
		showTwoBtnDialog(title, content, confirmString, "", listener, cancelAble);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param titleId    提示标题字符串id
	 * @param contentId  提示内容字符串id
	 * @param confirmId  确认按钮字符串id
	 * @param listener   按钮事件监听
	 * @param cancelAble
	 */
	public void showOneBtnDialog(int titleId, int contentId, int confirmId, CommonDialog.ButtonClickListener listener, boolean cancelAble) {
		showTwoBtnDialog(getString(titleId), getString(contentId), getString(confirmId), "", listener, cancelAble);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param titleId   提示标题字符串id
	 * @param contentId 提示内容字符串id
	 * @param confirmId 确认按钮字符串id
	 */
	public void showOneBtnDialog(int titleId, int contentId, int confirmId) {
		showTwoBtnDialog(getString(titleId), getString(contentId), getString(confirmId), "", null, true);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param title         提示标题
	 * @param content       提示内容
	 * @param confirmString 确认按钮
	 */
	public void showOneBtnDialog(String title, String content, String confirmString) {
		showTwoBtnDialog(title, content, confirmString, "", null, true);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param content       提示内容
	 * @param confirmString 确认按钮
	 */
	public void showOneBtnDialog(String content, String confirmString) {
		showTwoBtnDialog(null, content, confirmString, "", null, true);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param content 提示内容
	 */
	public void showOneBtnDialog(String content) {
		showTwoBtnDialog(null, content, getString(R.string.confirm), "", null, true);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param contentId 提示内容
	 * @param confirmId 确认按钮
	 */
	public void showOneBtnDialog(int contentId, int confirmId) {
		showTwoBtnDialog(null, getString(contentId), getString(confirmId), "", null, true);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param contentId 提示内容
	 */
	public void showOneBtnDialog(int contentId) {
		showTwoBtnDialog(null, getString(contentId), getString(R.string.confirm), "", null, true);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param content       提示内容
	 * @param confirmString 确认按钮
	 */
	public void showOneBtnDialog(String content, String confirmString, CommonDialog.ButtonClickListener listener) {
		showTwoBtnDialog(null, content, confirmString, "", listener, true);
	}

	private void dismissCommonDialog() {
		if (mDialog != null && mDialog.isShowing()) {
			mDialog.dismiss();
		}
	}

	@Override
	protected void initProgressDialog(boolean cancelable) {
		Context ctx = getContext();
		if (ctx != null) {
			mLoadingDialog = new LoadingProgressDialog(getActivity(), R.style.loading_progress_dialog, "");
			mLoadingDialog.setCanceledOnTouchOutside(cancelable);
			mLoadingDialog.setCancelable(cancelable);
			mLoadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
				@Override
				public void onCancel(DialogInterface dialog) {
					mLoadingDialog.dismiss();
				}
			});
		}
	}
}
